<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px">
			<el-form-item label="会员卡名称" prop="name">
				<el-input v-model="form.name" placeholder="请输入会员卡名称" clearable></el-input>
			</el-form-item>
			<el-form-item label="会员卡类型" prop="type">
				 <el-radio-group v-model="form.type">
				    <el-radio :label="1">月卡</el-radio>
				    <el-radio :label="2">年卡</el-radio>
				    <el-radio :label="3">永久</el-radio>
					<el-radio :label="4">日卡</el-radio>
					<el-radio :label="5">周卡</el-radio>
					<el-radio :label="6">季卡</el-radio>
				  </el-radio-group>
			</el-form-item>
			<el-form-item label="原价" prop="price">
				<el-input-number v-model="form.price" controls-position="right" :precision="2" :min="1"
					style="width: 100%;"></el-input-number>
			</el-form-item>
			<el-form-item label="售价" prop="sell_price">
				<el-input-number v-model="form.sell_price" controls-position="right" :precision="2" :min="1"
					style="width: 100%;"></el-input-number>
			</el-form-item>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="form.remark" placeholder="请输入备注" type="textarea" clearable></el-input>
			</el-form-item>
			<el-form-item label="状态" prop="status">
				<el-select v-model="form.status" filterable style="width: 100%">
					<el-option v-for="(item, key) in statuss" :key="key" :label="item" :value="key" />
				</el-select>
			</el-form-item>
			<el-form-item label="排序" prop="sort">
				<el-input-number v-model="form.sort" controls-position="right" :min="1" style="width: 100%;"></el-input-number>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	import {
		defineAsyncComponent
	} from 'vue';
	const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));
	export default {
		emits: ['success', 'closed'],
		components: {
			scEditor
		},
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					id: "",
					name: "",
					type: 1,
					price: '',
					sell_price: '',
					status: '0',
					remark: '',
					sort: '1'
				},
				types: {
					'1': '月卡',
					'2': '年卡',
					'3': '永久'
				},
				statuss: {
					'0': '启用',
					'1': '禁用',
				},
				//验证规则
				rules: {
					name: [{
						required: true,
						message: '请输入会员卡名称'
					}],
					price: [{
						required: true,
						message: '请输入原价'
					}],
					sell_price: [{
						required: true,
						message: '请输入售价'
					}],
					// remark: [{
					// 	required: true,
					// 	message: '请输入备注'
					// }]
				}
			}
		},
		mounted() {
		},
		methods: {
			//显示
			open(mode = 'add') {
				this.mode = mode;
				this.visible = true;
				return this
			},
			timeChange(value) {
				this.form.start_time = value[0]
				this.form.end_time = value[1]
			},
			//表单提交方法
			async submit() {
				var valid = await this.$refs.dialogForm.validate().catch(() => {})
				if (!valid) {
					return false
				}
				this.isSaveing = true;
				let form = JSON.parse(JSON.stringify(this.form))
				var res;
				if (this.form.id) {
					res = await this.$API.marketing.MemberCard.update.post(form);
				} else {
					res = await this.$API.marketing.MemberCard.create.post(form);
				}

				this.isSaveing = false;
				if (res.code == 200) {
					if (this.mode == 'add') {
						this.form.id = res.data
					}
					let form = JSON.parse(JSON.stringify(this.form))
					this.$emit('success', form, this.mode)
					this.visible = false;
					this.$message.success("操作成功")
				} else {
					this.$alert(res.message, "提示", {
						type: 'error'
					})
				}
			},
			//表单注入数据
			setData(data) {
				this.form.id = data.id
				this.form.name = data.name
				this.form.status = data.status.toString()

				this.form.type = data.type
				this.form.price = data.price
				this.form.sell_price = data.sell_price
				this.form.remark = data.remark
				this.form.sort = data.sort
				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				//Object.assign(this.form, data)
			}
		}
	}
</script>

<style>
</style>
